/*
 * User.cpp
 *
 *  Created on: 2013. 8. 5.
 *      Author: jjaehuny
 */



/*
 * user.h
 *
 *  Created on: 2013. 7. 19.
 *      Author: jjaehuny
 */

#include "User.h"
#include "Static.h"

namespace CacheServer {

User::User() : _userSeq(0), _bKilled(false), _channelKey(0)
{
	_monitorTimer = new ::Dispatcher::Timer();
}

User::~User()
{
	delete _monitorTimer;
}

void
User::Initialize(int channelKey)
{
	_userSeq = 0;
	_bKilled = false;
	_channelKey = channelKey;

	_userInfo.userseq = 0;

	for (int i = 0; i < 8; i++)
	{
		_userInfo.array_Slot[i].open = false;
		_userInfo.array_Slot[i].character_seq = 0;
		_userInfo.array_Slot[i].makeCharacter = false;
		_userInfo.array_Slot[i].remainStatResetCount = 0;
		_userInfo.array_Slot[i].classtype = 0;
		_userInfo.array_Slot[i].level = 0;
		_userInfo.array_Slot[i].reg_date = 0;
	}

	//_userInfo.zen = 0;
	_userInfo.main_slot_index = -1;
	_userInfo.nickname.clear();
	_userInfo.profile_url.clear();
	_userInfo.score = 0;
	_userInfo.heart_blocked = 0;
	_userInfo.invite_blocked = 0;
	_userInfo.profile_opened = 0;
	_userInfo.company = 0;
	_userInfo.sale_code = 0;
	_userInfo.u8intro_state = 0;
	_userInfo.wp_last_week = 0;
	_userInfo.wp_cur_week = 0;
	_userInfo.wp_reg_date = 0;
	_userInfo.wp_last_reg_date = 0;
}

void User::OnResponse(::Database::Command * pCommand) {}
void User::OnError(::Database::Command * pCommand, const char* err, int errCode) {}
void User::OnTime()
{
	if (false == _bKilled)
		return;

	int mod = _userInfo.userseq % s_Mode;
	MAP & clsMap = Static::Instance().s_clsTreeVector[mod];
	MAP::iterator itr = clsMap.find(_userInfo.userseq);
	if (clsMap.end() == itr)
	{
		LOG(DEV, "not found user seq : ", _userInfo.userseq );
		return;
	}

	LOG(DEV, "erase user seq : ", _userInfo.userseq );
	clsMap.erase(_userInfo.userseq);
}

UserInfo &		User::GetUserInfo() { return _userInfo; }
void 			User::SetUserInfo(UserInfo userInfo) { _userInfo = userInfo; }

void 	User::SetUserSeq(int userSeq) { _userSeq = userSeq; }
int 	User::GetUserSeq() { return _userSeq; }

void 	User::SetTimer()
{
	LOG(DEV, "SetTimer found user seq : ", _userSeq, " bKilled : ", _bKilled);

	if (true == _bKilled)
		return;

	_bKilled = true;
	_monitorTimer->SetTimer(1000 * 60 * 30, std::bind( & User::OnTime, this));
	//_monitorTimer->SetTimer(5000, std::bind( & User::OnTime, this));
}

void	User::SetRandomTime()
{
	if (true == _bKilled)
		return;

	_bKilled = true;
	int time = rand() % 30;
	int time2 = ( 1000 * 60 ) * ( 30 + time) ;
	_monitorTimer->SetTimer(time2, std::bind( & User::OnTime, this));
}

void 	User::KillTimer() { _bKilled = false; }

} /* namespace cacheserver */


